package com.java2018.springmvc.user.service;

import com.java2018.springmvc.user.dao.UserMapper;
import com.java2018.springmvc.user.model.User;
import com.java2018.springmvc.utils.entity.JsonResponse;
import com.java2018.springmvc.utils.entity.SessionKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 获得用户
     * @return 单个用户实体
     */
    @Override
    public User getUser(User user) {
//        System.out.println(userMapper.getUser(user));
        return userMapper.getUser(user);
    }

    /**
     * 登录操作
     * @return 登录状态信息
     */
    @Override
    public JsonResponse login(User user, HttpSession session) {
        User theUser = getUser(user);
        JsonResponse jsonResponse = new JsonResponse();

        if(theUser!=null){
            session.setAttribute(SessionKey.LOGIN_USER_INFO,user);
            jsonResponse.setError(false);
            jsonResponse.setMsg("登录成功");
            jsonResponse.addData("user",user);
            jsonResponse.addData("markUrl",session.getAttribute("markUrl"));

        }else{
            jsonResponse.setMsg("登录失败，失败原因：用户名或者密码错误");
        }
        return jsonResponse;
    }


}
